package com.chenfeng.ego.sso.service.impl;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chenfeng.ego.commons.pojo.EgoResult;
import com.chenfeng.ego.commons.util.CookieUtils;
import com.chenfeng.ego.commons.util.IDUtils;
import com.chenfeng.ego.commons.util.JsonUtils;
import com.chenfeng.ego.mapper.TbUserMapper;
import com.chenfeng.ego.pojo.TbUser;
import com.chenfeng.ego.pojo.TbUserExample;
import com.chenfeng.ego.pojo.TbUserExample.Criteria;
import com.chenfeng.ego.sso.dao.JedisDao;
import com.chenfeng.ego.sso.service.UserService;
@Service
public class UserServiceImpl implements UserService
{
    @Resource
	private TbUserMapper usermapper;
    @Resource
    private JedisDao dao;
	@Override
	//当用户登录的时候要创建cookie 存值为 uuid   uuid将最为 redis的key内存  user对象      usr对象经过工具类转换
	public EgoResult login(TbUser tb,HttpServletRequest req,HttpServletResponse res)
	{
		//spring自带的md5加密
		tb.setPassword(DigestUtils.md5DigestAsHex(tb.getPassword().getBytes()));
		EgoResult ego = new EgoResult();
		//登录验证
		TbUserExample example = new TbUserExample();
		example.createCriteria().andUsernameEqualTo(tb.getUsername()).andPasswordEqualTo(tb.getPassword());
		List<TbUser> list = usermapper.selectByExample(example);
		if(list!=null&&list.size()>0)
		{
			//成功
			String uuid = UUID.randomUUID().toString();
			//uuid所代表的key的value存放了用户对象的信息
			dao.set("user-"+uuid , JsonUtils.objectToJson(list.get(0)));
			System.out.println(list.get(0));
			dao.expire("user-"+uuid , 30*60);
			CookieUtils.setCookie(req, res, "TT_TOKEN", uuid,30*60);
			ego.setStatus(200);
			ego.setData(uuid);
		}else
			//失败
		{
			ego.setStatus(400);
		}
		return ego;
	}
	@Override
	public EgoResult getTokenResult(String token,HttpServletRequest req,HttpServletResponse res)
	{
		EgoResult ego = new EgoResult();
		String string = dao.get("user-"+token);
		ego.setStatus(200);
		ego.setData(JsonUtils.jsonToPojo(string, TbUser.class));
		//模拟session多久不用就失效
		dao.expire("user-"+token , 30*60);
		CookieUtils.setCookie(req, res, "TT_TOKEN", token,30*60);
		return ego;
	}
	@Override
	public EgoResult lgout(String token, HttpServletRequest req, HttpServletResponse res)
	{
		//删除的是浏览器中的cookie
		CookieUtils.deleteCookie(req, res, "TT_TOKEN");
		long index = dao.del("user-"+token);
		EgoResult ego = new EgoResult();
		if(index>0)
		{
			ego.setStatus(200);
		}else
		{
			ego.setStatus(400);
		}
		return ego;
	}
	@RequestMapping("user/showRegister")
	public String showresiger()
	{
		return "register";
	}
	//判断输入的数据是否可以
	@Override
	public EgoResult check(String user, int type)
	{
		TbUserExample example = new TbUserExample();
		Criteria c = example.createCriteria(); 
		if(type==1)
		{
			c.andUsernameEqualTo(user);
		}else if(type==2)
		{
			c.andPhoneEqualTo(user);
		}else if(type==3)
		{
			c.andPasswordEqualTo(user);
		}
		EgoResult er = new EgoResult();
		List<TbUser> list = usermapper.selectByExample(example);
		if(list!=null&&list.size()>0)
		{
		
			er.setStatus(400);
			er.setData(false);
			er.setMsg("error");
		}else
		{
			er.setStatus(200);
			er.setData(true);
			er.setMsg("Ok");
		}
		return er;
	}
	//用户注册插入数据
	@Override
	public EgoResult register(TbUser user)
	{
		EgoResult ego = new EgoResult();
		user.setCreated(new Date());
		user.setUpdated(new Date());
		user.setId(IDUtils.genItemId());
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		int i = usermapper.insert(user);
		if(i>0)
		{
			ego.setStatus(200);
			ego.setMsg("恭喜你注册成功");
		}else
		{
			ego.setStatus(400);
			ego.setMsg("抱歉注册失败");
		}
		return ego;
	}
	

}
